import org.junit.Test;

import java.io.*;
import java.util.Properties;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/3/31 17:04
 */

public class ClassLoaderTest {
    // 通过ClassLoader加载指定的配置文件
    @Test
    public void test1() throws IOException {
        Properties pros = new Properties();

        // 使用ClassLoader下的getSystemClassLoader()方法下的getResourceAsStream()来获取input stream
        // 通过类的加载器读取的文件的默认的路径为：当前module下的src下
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("info1.properties");

        pros.load(is);

        String name = pros.getProperty("name");
        String pwd = pros.getProperty("password");
        System.out.println(name + ": " + pwd);
    }

    // Properties：处理属性文件
    @Test
    public void test2() throws IOException {
        Properties pros = new Properties();

        File file = new File("info.properties");
        // 通过类的加载器读取的文件的默认的路径为：当前的module下
        FileInputStream is = new FileInputStream(file);

        pros.load(is);

        String name = pros.getProperty("name");
        String pwd = pros.getProperty("password");
        System.out.println(name + ": " + pwd);
    }
}
